package com.gengzp.common.enums;

import com.gengzp.common.constants.PermissionConstants;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName PermissionEnum
 * @Description 权限枚举类, 系统所有的权限都必须在此枚举类中定义
 * @Author gengzp
 * @Date 2025/5/16 22:24
 */
@Getter
public enum PermissionEnum {

    // ===================== 权限管理 =====================
    PERMISSION_MANAGE(PermissionConstants.PERMISSION_MANAGE, "权限管理", null),
    PERMISSION_READ(PermissionConstants.PERMISSION_READ, "权限读", PermissionConstants.PERMISSION_MANAGE),
    PERMISSION_WRITE(PermissionConstants.PERMISSION_WRITE, "权限写", PermissionConstants.PERMISSION_MANAGE),

    // ===================== 角色管理 =====================
    ROLE_MANAGE(PermissionConstants.ROLE_MANAGER, "角色管理", null),
    ROLE_READ(PermissionConstants.ROLE_READ, "角色读", PermissionConstants.ROLE_MANAGER),
    ROLE_WRITE(PermissionConstants.ROLE_WRITE, "角色写", PermissionConstants.ROLE_MANAGER),

    // ===================== 用户管理 =====================
    USER_MANAGE(PermissionConstants.USER_MANAGE, "用户管理", null),
    USER_READ(PermissionConstants.USER_READ, "用户读", PermissionConstants.USER_MANAGE),
    USER_WRITE(PermissionConstants.USER_WRITE, "用户写", PermissionConstants.USER_MANAGE),

    // ===================== nacos管理 =====================
    NACOS_MANAGE(PermissionConstants.NACOS_MANAGE, "服务治理", null),

    // ===================== 消息管理 =====================
    MESSAGE_MANAGE(PermissionConstants.MESSAGE_MANAGE, "消息管理", null),
    MESSAGE_READ(PermissionConstants.MESSAGE_READ, "消息读", PermissionConstants.MESSAGE_MANAGE),
    MESSAGE_WRITE(PermissionConstants.MESSAGE_WRITE, "消息写", PermissionConstants.MESSAGE_MANAGE),

    // ===================== 数据库管理 =====================
    DATABASE_MANAGE(PermissionConstants.DATABASE_MANAGE, "数据库管理", null),
    DATABASE_READ(PermissionConstants.DATABASE_READ, "数据库读", PermissionConstants.DATABASE_MANAGE),
    DATABASE_WRITE(PermissionConstants.DATABASE_WRITE, "数据库写", PermissionConstants.DATABASE_MANAGE),

    ;

    // 权限名称
    private final String permissionCode;
    // 权限名称
    private final String permissionName;
    // 父级权限名称
    private final String parentPermissionCode;

    PermissionEnum(String permissionCode, String permissionName, String parentPermissionCode) {
        this.permissionCode = permissionCode;
        this.permissionName = permissionName;
        this.parentPermissionCode = parentPermissionCode;
    }

    /**
     * 根据名称获取编码
     *
     * @param permissionCode 权限编码
     * @return 权限名称
     */
    public static String getNameByCode(String permissionCode) {
        for (PermissionEnum value : PermissionEnum.values()) {
            if (StringUtils.equals(value.permissionCode, permissionCode)) {
                return value.permissionName;
            }
        }
        return null;
    }

}